<!DOCTYPE html>

<html>
  <head>
    <!-- allow fancy checkmarks to show up -->
    <meta charset="UTF-8">

    <!-- complain about uncaught errors -->
    <script type="text/javascript">
    window.onerror = function() {
      document.documentElement.style.background = 'red';
    };
    </script>

    <!-- better living through jQuery -->
    <script type="text/javascript" src="support/jquery-1.5.2.js"></script>

    <!-- mocha test framework -->
    <script type="text/javascript" src="../node_modules/mocha/mocha.js"></script>
    <link rel="stylesheet" type="text/css" href="../node_modules/mocha/mocha.css" />

    <!-- home-grown assertions -->
    <script src="support/assert.js" type="text/javascript"></script>

    <!-- configure mocha and chai -->
    <script type="text/javascript">
      var post_xunit_to = Mocha.utils.parseQuery(location.search).post_xunit_to;

      mocha.setup({
        ui: 'tdd',
        reporter: post_xunit_to ? 'xunit' : 'html'
      });

      var xunit = '';
      Mocha.process.stdout.write = function(line) { xunit += line; };
    </script>

    <!-- include the library with the tests inlined -->
    <script id="mathquill" type="text/javascript" src="../build/mathquill.test.js"
      onerror="alert('couldn\'t find `build/mathquill.test.js`. Please run `make test` before opening this file.')">
    </script>

    <!-- include MathQuill-basic -->
    <link rel="stylesheet" type="text/css" href="../build/mathquill.css" />
    <script type="text/javascript" src="../build/mathquill-basic.js"></script>
    <script type="text/javascript">
      MQBasic = MathQuill.noConflict().getInterface(MathQuill.getInterface.MAX);
      MQ = MathQuill.getInterface(MathQuill.getInterface.MAX);
    </script>

  </head>

  <body>
    <h1>Unit Tests</h1>
    <div id="mocha"></div>
    <div id="mock"></div>

    <script type="text/javascript">
      teardown(function() { $('#mock').empty(); });
      var runner = mocha.run();

      if (post_xunit_to) {
        // the following is based on https://github.com/saucelabs-sample-scripts/JavaScript/blob/4946c5cf0ab7325dce5562881dba7c28e30989e5/reporting_mocha.js
        var failedTests = [];
        runner.on('fail', function(test, err) {
          function flattenTitles(test) {
            var titles = [];
            while (test.parent.title) {
              titles.push(test.parent.title);
              test = test.parent;
            }
            return titles.reverse();
          }

          failedTests.push({name: test.title, result: false, message: err.message, stack: err.stack, titles: flattenTitles(test) });
        });

        runner.on('end', function() {
          setTimeout(function() {
            $.post(post_xunit_to, xunit).complete(function() {
              window.mochaResults = runner.stats;
              window.mochaResults.reports = failedTests;
            });
          });
        });
      }
    </script>
  </body>
</html>
